package 力扣;

import java.time.Instant;
import java.util.*;

/**
 * @author yyq
 * @create 2022-03-17 10:05
 */
public class leetcode15 {
    public static void main(String[] args) {
        int[] nums=new int[]{-1,0,1,2,-1,-4};
        threeSum(nums);
    }
    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> lists=new ArrayList<>();
        if(nums.length<3) return null;
        Arrays.sort(nums);
        if(nums[0]>0) return null;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]>0) break;
            if(i>0&&nums[i-1]==nums[i]) continue;
            int leftPoint=i+1;
            int rightPoint=nums.length-1;
            while(true){
                if(leftPoint>=rightPoint) break;
                if(nums[i]+nums[leftPoint]+nums[rightPoint]==0){
                    List<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[leftPoint]);
                    list.add(nums[rightPoint]);
                    lists.add(list);
                    while(true){
                        if(nums[leftPoint+1]==nums[leftPoint]&&leftPoint<rightPoint){
                            leftPoint++;
                            continue;
                        }
                        else {
                            leftPoint++;
                            break;
                        }
                    }
                   while(true){
                       if(nums[rightPoint]==nums[rightPoint-1]&&rightPoint>leftPoint){
                           rightPoint--;
                           continue;
                       }else {
                           rightPoint--;
                           break;
                       }
                   }
                    continue;
                }
                if(nums[i]+nums[leftPoint]+nums[rightPoint]<0&&rightPoint>leftPoint){
                    leftPoint++;
                    continue;
                }
                if(nums[i]+nums[leftPoint]+nums[rightPoint]>0&&rightPoint>leftPoint){
                    rightPoint--;
                    continue;
                }
            }

        }
        return lists;

    }


}


